package com.os;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import mapgenerator.Main;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 *
 * @author Sajarwo Anggai
 */
public class OSDialogBoundingBox extends JDialog implements ActionListener {

    private JButton Cancel;
    private JFormattedTextField Height;
    private JButton OK;
    private JFormattedTextField PosX;
    private JFormattedTextField PosY;
    private JFormattedTextField Width;
    private JLabel jLabelHeight;
    private JLabel jLabelPosX;
    private JLabel jLabelPosY;
    private JLabel jLabelWidth;
    private Main comp;
    private OSBoundingBox osb;
    private JLabel jLabelBackgrond;

    public OSDialogBoundingBox(Main comp) {
        this.comp = comp;
        initComponents();
    }

    private void initComponents() {
        jLabelPosX = new JLabel();
        jLabelPosY = new JLabel();
        jLabelWidth = new JLabel();
        jLabelHeight = new JLabel();
        PosX = new JFormattedTextField(0);
        PosY = new JFormattedTextField(0);
        Width = new JFormattedTextField(0);
        Height = new JFormattedTextField(0);
        OK = new JButton();
        Cancel = new JButton();
        jLabelBackgrond = new JLabel();

        setTitle("Add New Bounding Box");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(250, 200));
        setLocationByPlatform(true);
        setResizable(false);
        getContentPane().setLayout(new AbsoluteLayout());

        jLabelPosX.setText("Pos X:");
        getContentPane().add(jLabelPosX, new AbsoluteConstraints(10, 50, -1, -1));

        jLabelPosY.setText("Pos Y:");
        getContentPane().add(jLabelPosY, new AbsoluteConstraints(10, 80, -1, -1));

        jLabelWidth.setText("Width:");
        getContentPane().add(jLabelWidth, new AbsoluteConstraints(120, 50, -1, -1));

        jLabelHeight.setText("Height:");
        getContentPane().add(jLabelHeight, new AbsoluteConstraints(120, 80, -1, -1));

        PosX.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        getContentPane().add(PosX, new AbsoluteConstraints(50, 50, 60, -1));

        PosY.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        getContentPane().add(PosY, new AbsoluteConstraints(50, 80, 60, -1));

        Width.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        getContentPane().add(Width, new AbsoluteConstraints(160, 50, 60, -1));

        Height.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        getContentPane().add(Height, new AbsoluteConstraints(160, 80, 60, -1));

        OK.setText("OK");
        OK.addActionListener(this);
        getContentPane().add(OK, new AbsoluteConstraints(97, 120, 60, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(this);
        getContentPane().add(Cancel, new AbsoluteConstraints(160, 120, -1, -1));

        jLabelBackgrond.setIcon(new ImageIcon(getClass().getResource("/com/images/DialogAddBoundingboxSprite.png"))); // NOI18N
        getContentPane().add(jLabelBackgrond, new AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }

    public void setBound(OSBoundingBox osb) {
        PosX.setValue(osb.x);
        PosY.setValue(osb.y);
        Width.setValue(osb.width);
        Height.setValue(osb.height);
        this.osb = osb;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getSource() == OK) {
                int x = Integer.parseInt("" + PosX.getValue());
                int y = Integer.parseInt("" + PosY.getValue());
                int w = Integer.parseInt("" + Width.getValue());
                int h = Integer.parseInt("" + Height.getValue());
                OSBoundingBox bound = new OSBoundingBox();
                bound.setBounds(x, y, Math.abs(w), Math.abs(h));
                if (osb != null) {
                    comp.osControlPanel.editBoundingBox(osb, bound);
                } else {
                    comp.osControlPanel.addBoundingBox(bound);
                }
            }
        } catch (Exception err) {
        }
        dispose();
    }
}
